import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';

const instance: AxiosInstance = axios.create({
  baseURL: 'http://127.0.0.1:8888/api/private/v1',
  timeout: 5000,
  responseType: 'json'
});
instance.interceptors.request.use(config => {
  NProgress.start(); // 请求开始时显示进度条
  const localToken = localStorage.getItem('react-jwt-token');
  if (localToken) {
    config.headers['Authorization'] = localToken;
  }
  
  return config;
}, error => {
  return Promise.reject(error);
});

instance.interceptors.response.use((response: AxiosResponse) => {

  NProgress.done(); // 响应完成时停止进度条
  return response;
}, (error: AxiosError) => {
  NProgress.done(); // 响应错误时停止进度条
  return Promise.reject(error);
});
const {request,get,post} = instance;
//暴露axios实例
export {request,get,post,instance}; 